#pragma once

#include "Texture.hpp"

class Material {
	
	friend class ModelNode;
	
	friend class Engine;
	
private:

	void bind();
	
	Texture* texture = 0;
	
	Texture* normalTexture = 0;
	
	bool enableTextureFlag = true;
	
	bool enableNormalTextureFlag = true;
	
	glm::vec2 textureOffset = glm::vec2(0,0);
	
public:

	void setTextureOffsetX(const float x);
	
	void setTextureOffsetY(const float y);

	void setTextureOffset(const glm::vec2& textureOffset);
	
	void setTextureOffset(float x, float y);
	
	const glm::vec2& getTextureOffset() const;

	void setNormalTexture(Texture* texture);
	
	void enableNormalTexture();
	
	void disableNormalTexture();
	
	const bool isNormalTextureEnable() const;

	void setTexture(Texture* texture);
	
	void enableTexture();

	void disableTexture();

	const bool isTextureEnable() const;

public:

	glm::vec4 diffuse = glm::vec4(1,1,1,1);

	GLfloat shininess = 64; // 0..128
	
	GLfloat emission = 0; // 0..1
	
} defaultMaterial;

void Material::setTextureOffsetX(const float x){
	textureOffset.x = x;
}
	
void Material::setTextureOffsetY(const float y){
	textureOffset.y = y;
}
	
void Material::setTextureOffset(const glm::vec2& textureOffset){
	Material::textureOffset = textureOffset;
}
	
void Material::setTextureOffset(float x, float y){
	textureOffset.x = x;
	textureOffset.y = y;
}
	
const glm::vec2& Material::getTextureOffset() const{
	return textureOffset;
}


void Material::enableNormalTexture(){
	enableNormalTextureFlag = true;
}
	
void Material::disableNormalTexture(){
	enableNormalTextureFlag = false;
}
	
const bool Material::isNormalTextureEnable() const{
	return enableNormalTextureFlag;
}

void Material::setNormalTexture(Texture* texture){
	normalTexture = texture;
}

void Material::enableTexture(){
	enableTextureFlag = true;
}

void Material::disableTexture(){
	enableTextureFlag = false;
}

const bool Material::isTextureEnable() const {
	return enableTextureFlag;
}

void Material::setTexture(Texture* texture){
	Material::texture = texture;
}

void Material::bind(){
	if (texture) {
		if (enableTextureFlag) {
			glActiveTexture(GL_TEXTURE0);
			texture->bind();
			engine.shader.enableMaterialTexture();
		} else 
			engine.shader.disableMaterialTexture();
	} else {
		engine.shader.disableMaterialTexture();
	}
	if (normalTexture) {
		if (enableNormalTextureFlag){
			glActiveTexture(GL_TEXTURE1);
			normalTexture->bind();
			engine.shader.enableMaterialNormalTexture();
		} else
			engine.shader.disableMaterialNormalTexture();
	}  else {
		engine.shader.enableMaterialNormalTexture();
	}
	engine.shader.setMaterialDiffuse(diffuse);
	engine.shader.setMaterialShininess(shininess);
	engine.shader.setMaterialEmission(emission);
	engine.shader.setTextureOffset(textureOffset);
}
